<?php

require '../!inc.php';
$tempDir = $oRegistry->Ini->TEMP_DIR;
echo ' $tempDir: ' . $tempDir;

$cookieFile = $tempDir . 'github.txt';

$version = curl_version();
$ssl_supported = ($version['features']);
echo '$ssl_supported: ' . $ssl_supported;
echo ' flag: ' . CURL_VERSION_SSL;


$aHeaders = array(
    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    'Accept-Language' => 'en-US,en;q=0.8',
    'Cache-Control' => 'no-cache'
);

$body = null;
try {
    $oCurl = new \StudyBuddy\Curl();
    $oCurl->setHeaders($aHeaders)
            ->setCookieFile($cookieFile);
    $oCurl->getDocument('https://github.com/login');
    $body = $oCurl->getResponseBody();

    //echo $body;
    //exit;
} catch (\Exception $e) {

    $s = 'E: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on ' . $e->getLine();
    exit($s);
}

$charset = $oCurl->getCharset();
echo ' $charset: ' . $charset;
/* $info = $oCurl->getCurlInfo();
  print_r($info); */

if (!$body) {
    exit('something is wrong, no body');
}

echo __LINE__ . ' ';
$oHTML = new \StudyBuddy\String\HTMLString();
/**
 * Always use @ to suppress errors in loadHTML!
 * Unless html has previously been fixed with tidy!
 * 
 */
$res = @$oHTML->loadHTML($body);
echo 'loaded: ' . $res;
//echo 'body: '.(string)$oHTML->saveHTML();
$forms = $oHTML->xpath('//form[@action = "/session"]');
if (1 !== $forms->length) {
    exit('no login form or more than one');
}

$oForm = $forms->item(0); // StudyBuddy\Dom\Element
$formUrl = $oForm->getAttribute('action');
echo ' $formUrl: ' . $formUrl . ' ';

$inputs = $oHTML->xpath('//form[@action = "/session"]//input');
if ($inputs->length < 2) {
    exit('No form inputs found');
}

$aInputs = array();
for ($i = 0; $i < $inputs->length; $i+= 1) {
    $item = $inputs->item($i);
    $name = $item->getAttribute('name');
    $aInputs[$name] = $item->getAttribute('value');
}

echo ' $aInputs : ' . print_r($aInputs, 1);

$aInputs['login'] = 'wer';
$aInputs['password'] = 'werwe2t';

function get_page($url, $ref = '') {
    global $aHeaders;
    global $cookieFile;
    $oCurl = new \StudyBuddy\Curl();
    $oCurl->setHeaders($aHeaders)
            ->setCookieFile($cookieFile)
            ->setReferrer($ref)
            ->getDocument($url);

    $body = $oCurl->getResponseBody();

    echo ' body: ' . $body;
}

try {
    $oCurl = new \StudyBuddy\Curl();
    $oCurl->setHeaders($aHeaders)
            ->setCookieFile($cookieFile)
            ->setReferrer('https://github.com/login');
    $oCurl->getDocument('https://github.com' . $formUrl, null, null, array('formVars' => $aInputs, 'redirect' => 4));

    $body = $oCurl->getResponseBody();

    echo ' body: ' . $body;
} catch (\Exception $e) {

    $info = $oCurl->getCurlInfo();
    echo 'INFO: ' . print_r($info, 1);

    if ($e instanceof \StudyBuddy\HttpRedirectException) {
        $newUrl = $e->getNewURI();
        get_page($newUrl, $info['url']);
    }
    $s = 'E: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on ' . $e->getLine();
    exit($s);
}
